﻿using IOP.SgrA.GLSL;
using IOP.SgrA.SilkNet.Vulkan;
using IOP.SgrA.SilkNet.Vulkan.Scripted;
using Silk.NET.Vulkan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample115
{
    [ShaderVersion("460")]
    [GLSLExtension("GL_ARB_separate_shader_objects : enable")]
    [GLSLExtension("GL_ARB_shading_language_420pack : enable")]
    public partial class CommonVert : GLSLVert
    {
        [PushConstant(64, 0)]
        public ConstantVals myConstantVals { get; set; }
        [UniformBuffer(0, 0, DescriptorSetTarget.RenderObject)]
        public BufferVals myBufferVals { get; set; }

        [VertInput(0, Format.R32G32B32Sfloat)]
        public vec3 aPosition {  get; set; }
        [VertInput(1, Format.R32G32Sfloat)]
        public vec2 inTexCoor {  get; set; }
        [ShaderOutput(0)]
        public vec2 outTexCoor { get; set; }

        public override void main()
        {
            float pi = 3.1415926f; //圆周率近似值
            float centerX = 0.0f; //中心点的X 坐标
            float centerY = -15; //中心点的Y 坐标
            float currX = aPosition.x; //当前点的X 坐标
            float currY = aPosition.y; //当前点的Y 坐标
            float spanX = currX - centerX; //当前X 偏移量
            float spanY = currY - centerY; //当前Y 偏移量
            float currRadius = sqrt(spanX * spanX + spanY * spanY); //计算距离
            float currRadians; //当前点与X 轴正方向的夹角
            if (spanX != 0.0)
            { //一般情况
                currRadians = atan(spanY, spanX);
            }
            else
            { //特殊情况
                currRadians = spanY > 0.0f ? pi / 2.0f : 3.0f * pi / 2.0f;
            }
            float resultRadians = currRadians + myBufferVals.ratio * currRadius; //计算出扭曲后的角度
            float resultX = centerX + currRadius * cos(resultRadians); //计算结果点的X 坐标
            float resultY = centerY + currRadius * sin(resultRadians); //计算结果点的Y 坐标
            outTexCoor = inTexCoor; //传输到片元着色器的纹理坐标
            gl_Position = myConstantVals.mvp * new vec4(resultX, resultY, 0.0f, 1.0f); //计算顶点最终位置
        }
    }
}
